﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Workflow.Alerts.DAL;
using System.Text;
using System.Globalization;
using workflow.Dal;

namespace Workflow.Alerts
{
    public class SavePresenter : Presenter
    {
         public SavePresenter(IView _view, ISessionProvider session)
            : base(_view, session)
        {
            
        }

        public void SaveSentAlert(Alert alert, WorkflowEntitySet context)
        {
            var dbAlert = context.Alert.Where(a => a.id == alert.id).First();
            dbAlert.currentstatus = alert.currentstatus;
            AlertHistory ah = new AlertHistory();
            context.AddToAlertHistory(ah);
            ah.Alert = dbAlert;
            ah.date = DateTime.Now;
            ah.Person = context.Person.Where(p => p.id == CurrentUser.Id).First();
            ah.status = alert.currentstatus;
        }

        public void SaveNewAlert(Alert alert, WorkflowEntitySet context)
        {
            var dbAlert = context.Alert.Include("Comments1").Where(a => a.id == alert.id).First();
            dbAlert.estimatestartdate = alert.estimatestartdate;
            dbAlert.estimateenddate = alert.estimateenddate;
            dbAlert.currentstatus = alert.currentstatus;
            if ((WorkflowEnums.Status)alert.currentstatus == WorkflowEnums.Status.Rejected)
                dbAlert.archived = true;

            var tempComments = alert.Comments1.Where(c => c.id == 0).ToList();
            foreach (var tempComment in tempComments)
            {
                var comments = new Comments();
                context.AddToComments(comments);
                comments.Alert = dbAlert;
                comments.comment = tempComment.comment;
                comments.createddatetime = tempComment.createddatetime;
                comments.Person = context.Person.Where(p => p.id == tempComment.Person.id).First();
            }

            AlertHistory ah = new AlertHistory();
            context.AddToAlertHistory(ah);
            ah.Alert = dbAlert;
            ah.date = DateTime.Now;
            ah.Person = context.Person.Where(p => p.id == CurrentUser.Id).First();
            ah.status = alert.currentstatus;
        }

        public void SaveExecutedAlert(Alert alert, WorkflowEntitySet context)
        {
            var dbAlert = context.Alert.Include("AlertMaterials").Include("Comments1").Where(a => a.id == alert.id).First();
            dbAlert.actualarea = alert.actualarea;
            dbAlert.actualenddate = alert.actualenddate;
            dbAlert.actualstartdate = alert.actualstartdate;
            dbAlert.currentstatus = alert.currentstatus;

            if (alert.Contractor != null)
            {
                dbAlert.Contractor = context.Contractor.Where(c => c.id == alert.Contractor.id).First();
                dbAlert.contractorname = alert.Contractor.name;
            }
            else
            {
                dbAlert.Contractor = null;
                dbAlert.contractorname = string.Empty;
            }

            var tempComments = alert.Comments1.Where(c => c.id == 0).ToList();
            foreach (var tempComment in tempComments)
            {
                var comments = new Comments();
                context.AddToComments(comments);
                comments.Alert = dbAlert;
                comments.comment = tempComment.comment;
                comments.createddatetime = tempComment.createddatetime;
                comments.Person = context.Person.Where(p => p.id == tempComment.Person.id).First();
            }

            List<AlertMaterials> listOfAlertMaterials = Session["TempAlertMaterials"] as List<AlertMaterials>;

            if (listOfAlertMaterials!=null&&listOfAlertMaterials.Any())
            {
                var alertMaterials = listOfAlertMaterials.Where(am => am.Alert.id == alert.id).ToList();
                var oldAlertMaterials = dbAlert.AlertMaterials.ToList<AlertMaterials>();
                //clear exiting materials

                foreach (var am in oldAlertMaterials)
                {
                    context.DeleteObject(am);
                }
                dbAlert.AlertMaterials.Clear();

                foreach (var alerMaterial in alertMaterials)
                {
                    var dbAlertMaterial = new AlertMaterials();
                    dbAlertMaterial.Alert = dbAlert;
                    dbAlertMaterial.Materials = context.Materials.Where(m => m.id == alerMaterial.Materials.id).First();
                    dbAlertMaterial.price = alerMaterial.price;
                    dbAlertMaterial.quantity = alerMaterial.quantity;
                    dbAlertMaterial.uitstel = alerMaterial.uitstel;
                    dbAlertMaterial.uitvoerd = alerMaterial.uitvoerd;
                    context.AddToAlertMaterials(dbAlertMaterial);
                    listOfAlertMaterials.Remove(alerMaterial);
                }
                Session["TempAlertMaterials"] = listOfAlertMaterials;
            }

            AlertHistory ah = new AlertHistory();
            context.AddToAlertHistory(ah);
            ah.Alert = dbAlert;
            ah.date = DateTime.Now;
            ah.Person = context.Person.Where(p => p.id == CurrentUser.Id).First();
            ah.status = alert.currentstatus;

        }
       
    }
}
